/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;


import java.util.*;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class AddEquip extends JFrame implements ActionListener
{
    public PreparedStatement pstmt;
    public Statement stmt1, stmt2;
    public ResultSet rset;
    public String query1, query2;
    public int n;
  //  private static int idnum = 1;
  //  private JLabel ID;
    private JLabel addequip;
    private JLabel header;
  //  private JLabel idtext;
    private JButton cancel;
    private JPanel editpanel;
    private JLabel name;
    private JTextField nametext;
    private JLabel price;
    private JTextField priceText;
    private JButton save;
    //private JLabel status;
    private JOptionPane savemsg;
    private JOptionPane cancelmsg;
    //private JRadioButton func, broken;
    //private ButtonGroup buttongrp;

    public AddEquip()
    {
        super("Brickroad Fitness - Add New Equipment");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

    private void initComponents() {

        header = new JLabel();
        editpanel = new JPanel();
        addequip = new JLabel();
      //  ID = new JLabel();
        name = new JLabel();
        price = new JLabel();
       // status = new JLabel();
      //  idtext = new JLabel();
        nametext = new JTextField();
        priceText = new JTextField();
        save = new JButton();
        cancel = new JButton();
        savemsg = new JOptionPane();
        cancelmsg = new JOptionPane();


       // func = new JRadioButton();
       // broken = new JRadioButton();
       // buttongrp = new ButtonGroup();

       // buttongrp.add(func);
       // buttongrp.add(broken);

        save.addActionListener(this);
        cancel.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        editpanel.setBackground(new Color(255, 255, 102));
        editpanel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        addequip.setFont(new Font("Arial", 3, 18)); // NOI18N
        addequip.setText("Add New Equipment");

       // ID.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
       // ID.setText("Equipment ID:");

        name.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        name.setText("Equipment Name:");

        price.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        price.setText("Price:");

       // status.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
       // status.setText("Status:");

       // idtext.setFont(new Font("Tahoma", 0, 14)); // NOI18N
       // idtext.setText(Integer.toString(idnum));

        nametext.setFont(new Font("Tahoma", 0, 14)); // NOI18N

        priceText.setFont(new Font("Tahoma", 0, 14)); // NOI18N

       // func.setBackground(new Color(255, 255, 102));
       // func.setFont(new Font("Tahoma", 1, 14)); // NOI18N
       // func.setSelected(true);
       // func.setText("Functional");

       // broken.setBackground(new Color(255, 255, 102));
       // broken.setFont(new Font("Tahoma", 1, 14)); // NOI18N
       // broken.setText("Broken");

        save.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        save.setText("SAVE");
        save.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cancel.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        cancel.setText("CANCEL");
        cancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        GroupLayout editpanelLayout = new GroupLayout(editpanel);
        editpanel.setLayout(editpanelLayout);
        editpanelLayout.setHorizontalGroup(
            editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(editpanelLayout.createSequentialGroup()
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(addequip))
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                           // .addComponent(ID)
                            .addComponent(name)
                            .addComponent(price))
                            //.addComponent(status))
                        /*.addGap(23, 23, 23)
                        .addGroup(editpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(editpanelLayout.createSequentialGroup()
                                .addComponent(func)
                                .addGap(18, 18, 18)
                                .addComponent(broken))*/
                        .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(nametext, GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                               // .addComponent(idtext, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
                                .addComponent(priceText, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(275, 275, 275)
                        .addComponent(save, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(cancel, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(150, Short.MAX_VALUE))
        );
        editpanelLayout.setVerticalGroup(
            editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(editpanelLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(addequip)
                .addGap(18, 18, 18)
               /* .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(ID)
                    .addComponent(idtext, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))*/
                .addGap(18, 18, 18)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(name)
                    .addComponent(nametext, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(price)
                    .addComponent(priceText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                /*.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(status)
                    .addComponent(func)
                    .addComponent(broken))*/
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(cancel, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(save, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                .addContainerGap())
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                .addComponent(editpanel, GroupLayout.Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(header, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 890, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editpanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        cnd a=new cnd();
        int n = 0;
        int b = 1;
        int idnumber=0;

        if (event == save)
        {
            if( !(nametext.getText().isEmpty())
                && !(priceText.getText().isEmpty()))
                //&& buttongrp.getSelection() != null)
            {
                int ans;
                ans = savemsg.showConfirmDialog(this, "Are you sure you want to add the said equipment?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (ans == 0)
                {
                    a.connect();
                    try {

                       stmt2 = a.con2.createStatement();
                       //while (rset.next())
                       //{
                             n++;
                             query1= "SELECT MAX(eqID) FROM `equipment`";
                             stmt1 = a.con1.createStatement();
                             rset = stmt1.executeQuery(query1);
                             rset.next();
                             System.out.println(idnumber);
                             idnumber= rset.getInt("max(eqID)");
                             //idnumber=Integer.parseInt(temp);
                             idnumber=idnumber+1;
                             System.out.println(idnumber);
                             query2 = "INSERT INTO `Equipment`(`eqID`, `eqName`, `eqPrice`) " +
                                           "VALUES('" + idnumber + "', '" +nametext.getText() + "', '" + priceText.getText() +"')";
                             stmt2.executeUpdate(query2);
                             a.disconnect();
                             savemsg.showMessageDialog(this, "Save successful!", "Save", JOptionPane.INFORMATION_MESSAGE);
                             dispose();

                       //}
                       System.out.println(n + " records from equipment table were successfully copied.");
                        } catch (Exception ex) {
                        System.out.println("Error copying records from equipment table. " + ex.getMessage());
                        }
                }
            }

            else
                savemsg.showMessageDialog(this, "Incomplete fields", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else if(event == cancel)
        {
            int ans;
            ans = cancelmsg.showConfirmDialog(this, "Are you sure you want to cancel? All unsaved information will be lost.", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(ans == 0)
            {
                dispose();
            }
        }
    }

}



